import { touchEventData } from "./eventListen/event";

let dragStartPos = { x: 0, y: 0 };
let dragStartPos1 = { x: 0, y: 0 };
let lastPinchDistance = 0;


// 计算两指间距离
function getDistance(touch1: Touch, touch2: Touch) {
    const dx = touch2.clientX - touch1.clientX;
    const dy = touch2.clientY - touch1.clientY;
    return Math.sqrt(dx * dx + dy * dy);
}

export function dragStartFn(e: TouchEvent) {
    if (e.touches.length === 1) {
        lastPinchDistance = 0;
        // 单指滑动
        dragStartPos = { x: e.touches[0].clientX, y: e.touches[0].clientY };
        lastPinchDistance = 0;
    } else if (e.touches.length === 2) {
        // 双指缩放
        lastPinchDistance = getDistance(e.touches[0], e.touches[1]);
        dragStartPos = { x: e.touches[0].clientX, y: e.touches[0].clientY };
        dragStartPos1 = { x: e.touches[1].clientX, y: e.touches[1].clientY };
    }
}

export function dragEndFn(e: TouchEvent, fn: (a: touchEventData) => void) {
    if (e.changedTouches.length === 1 && lastPinchDistance === 0) {
        // 单指滑动
        const touch = e.changedTouches[0];
        const endPos = { x: touch.clientX, y: touch.clientY };
        const dx = endPos.x - dragStartPos.x;
        const dy = endPos.y - dragStartPos.y;
        const distance = Math.sqrt(dx * dx + dy * dy);
        fn({
            type: 'swipe',
            start: [dragStartPos],
            end: [endPos],
            value: distance
        })
    } else if (e.changedTouches.length === 2 || (e.changedTouches.length === 1 && lastPinchDistance > 0)) {
        let touches = Array.from(e.changedTouches);
        if (touches.length < 2 && e.touches.length === 1) {
            // 有些设备touchend只剩1个点，补上另一个
            touches.push(e.touches[0]);
        }
        if (touches.length === 2) {
            const endDistance = getDistance(touches[0], touches[1]);
            const endPos = { x: touches[0].clientX, y: touches[0].clientY };
            const endPos1 = { x: touches[1].clientX, y: touches[1].clientY };
            const scale = endDistance / lastPinchDistance;
            fn({
                type: 'zoom',
                start: [dragStartPos, dragStartPos1],
                end: [endPos, endPos1],
                value: scale
            })
        }

    }
}



export function dragSwipeStartFn(e: TouchEvent | MouseEvent) {
    if (e instanceof TouchEvent) {
        dragStartPos = { x: e.touches[0].clientX, y: e.touches[0].clientY || e.clientY };
    } else {
        dragStartPos = { x: e.clientX, y: e.clientY };
    }
}

export function dragSwipeEndFn(e: TouchEvent | MouseEvent, fn: (a: touchEventData) => void) {
    let endPos = { x: 0, y: 0 };
    if (e instanceof TouchEvent) {
        const touch = e.changedTouches[0];
        endPos = { x: touch.clientX, y: touch.clientY }
    } else {
        endPos = { x: e.clientX, y: e.clientY }
    }
    const dx = endPos.x - dragStartPos.x;
    const dy = endPos.y - dragStartPos.y;
    const distance = Math.sqrt(dx * dx + dy * dy);
    fn({
        type: 'swipe',
        start: [dragStartPos],
        end: [endPos],
        value: distance
    })
}